// 获取token
import { getToken, getTokenExpired,removeToken, removeTokenExpired } from '@/utils/localStoreToken.js'
import CONFIG from '@/config.js'
// 
const http  = {
	request(config) {
		// 默认头
		config.header = {...config.header, ...{'Content-Type': 'application/json'}}|| {'Content-Type': 'application/json'}
		let token = getToken()
		if(token){
			config.header = {
				...config.header,
				token
			}
		}
		// 默认地址
		config.url = config.url.startsWith('https')? config.url : CONFIG.baseURL + config.url
		// 方法
		config.method = config.method|| 'GET'
		//请求体
		config.data = config.data || { }
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.url,
				data: config.data,
				header: config.header,
				method: config.method,
				success: (res) => {
					resolve(res.data)
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	},
	post(url,data,config) {
		return this.request({
			url,
			data,
			method: 'POST',
			...config
		})
	},
	get(url,config) {
		return this.request({
			url,
			method: 'GET',
			...config
		})
	}
}

export default http